package com.linzm.leetcode.primary.栈和队列;

import org.junit.Test;

import java.util.*;

/**
 * @Author zimingl
 * @Date 2022/10/7 1:46
 * @Description: TODO
 */
public class 栈和队列20有效的括号 {

    /**
     * 第一种情况，字符串里左方向的括号多余了 ，所以不匹配。
     * 第二种情况，括号没有多余，但是 括号的类型没有匹配上
     * 第三种情况，字符串里右方向的括号多余了，所以不匹配。
     */

    public boolean isValid(String s) {
        Deque<Character> deque = new LinkedList<>();
        char ch;
        for (int i = 0; i < s.length(); i++) {
            ch = s.charAt(i);
            //碰到左括号，就把相应的右括号入栈
            if (ch == '(') {
                deque.push(')');
            } else if (ch == '{') {
                deque.push('}');
            } else if (ch == '[') {
                deque.push(']');
            }
            // 第三种情况：遍历字符串匹配的过程中，栈已经为空了，没有匹配的字符了，说明右括号没有找到对应的左括号 return false
            // 第二种情况：遍历字符串匹配的过程中，发现栈里没有我们要匹配的字符。所以return false
            else if (deque.isEmpty() || deque.peek() != ch) {
                return false;
            } else {//如果是右括号判断是否和栈顶元素匹配
                deque.pop();
            }
        }
        //最后判断栈中元素是否匹配
        // 第一种情况：此时我们已经遍历完了字符串，但是栈不为空，说明有相应的左括号没有右括号来匹配，所以return false，否则就return true
        return deque.isEmpty(); // true if this collection contains no elements.
    }

    public boolean isValid2(String s) {
        Stack<Character> stack = new Stack<>();
        char ch = 'a';
        for (int i = 0; i < s.length(); i++) {
            ch = s.charAt(i);
            if (ch == '(') {
                stack.push(')');
            } else if (ch == '[') {
                stack.push(']');
            } else if (ch == '{') {
                stack.push('}');
            }
            // 右边多符号的，比如 s = ")"  到这里栈就是空的 stack.isEmpty()
            // 字符串不多不少，但是匹配不上 s = "([})" stack.peek() != ch
            else if (stack.isEmpty() || stack.peek() != ch) {
                return false;
            } else {
                stack.pop();
            }
        }
        // 如果为空则为true ，非空证明左边多符号了， s = "(()"
        return stack.isEmpty();
    }

    @Test
    public void test() {
/*        String s = "((){])";
        boolean valid = isValid2(s);
        System.out.println(valid);*/
        Deque<Character> deque2 = new LinkedList<>();
/*        deque2.push('a');
        deque2.push('b');
        deque2.push('c');
        System.out.println(deque2.pop());
        System.out.println("peek:" + deque2.peek());
        System.out.println(deque2.pop());
        System.out.println("peek:" + deque2.peek());
        System.out.println(deque2.pop());
        System.out.println("peek:" + deque2.peek());
        System.out.println(deque2.isEmpty());
        System.out.println("================================================");*/

        deque2.offer('a');
        deque2.offer('b');
        deque2.offer('c');
        System.out.println(deque2.poll());
        System.out.println("peek:" + deque2.peek());
        System.out.println(deque2.poll());
        System.out.println("peek:" + deque2.peek());
        System.out.println(deque2.poll());
        System.out.println("peek:" + deque2.peek());
        System.out.println(deque2.isEmpty());
        System.out.println("================================================");

        deque2.add('a');
        deque2.add('b');
        deque2.add('c');
        System.out.println(deque2.remove());
        System.out.println("peek:" + deque2.peek());
        System.out.println(deque2.remove());
        System.out.println("peek:" + deque2.peek());
        System.out.println(deque2.remove());
        System.out.println("peek:" + deque2.peek());
        System.out.println(deque2.isEmpty());
        System.out.println("================================================");

        Stack<Character> stack = new Stack<>();
        stack.push('a');
        stack.push('b');
        stack.push('c');

        System.out.println(stack.pop());
        System.out.println("peek:" + stack.peek());
        System.out.println(stack.pop());
        System.out.println("peek:" + stack.peek());
        System.out.println(stack.pop());
        System.out.println("peek:" + stack.peek());
        System.out.println(stack.isEmpty());
        System.out.println("================================================");


        Queue<Integer> deque = new ArrayDeque<>();
        System.out.println("add: " + deque.add(1));
        System.out.println("element: " + deque.element());
        System.out.println("remove: " + deque.remove());

        System.out.println("offer: " + deque.offer(1));
        System.out.println("peek: " + deque.peek());
        System.out.println("poll: " + deque.poll());

        System.out.println(deque.isEmpty());

    }

    @Test
    public void test2() {
        char ch = 'A';
        int foo = 65;
        boolean flag = true;
        System.out.println(flag ? ch : 1L);
        System.out.println(flag ? ch : 'o');
    }
}
